package com.shaoshaohuo.app.framework;

import android.content.Context;
import android.content.SharedPreferences.Editor;

import com.shaoshaohuo.app.SshApplication;

import java.util.ArrayList;

/**
 * SharedPreferences cache
 */
public final class SharedPreferencesHelper {

    public final static String TAG = "SharedPreferencesHelper";
    private final static String DATA_NAME = "tianmaoyitong";
    private final static String SPLITCHAR = ",";

    public enum Field {
        /** 用户名 */
        USER_NAME,
        /** 用户 token */
        USER_TOKEN,
        /** 用户 id */
        USER_ID,
        USER_REAL_NAME,
        USER_MOBILE,
        BAOXIAN_PRICE,
        BAOXIAN_MONEY,
        USER_IS_AUTH,
        CAR_IS_AUTH,
        PUSH_DEVICE_ALIAS,

        IS_COME_BACK, //是否返回角色选择
        SHOW_BANK_NAME, //银行的名字
        SHOW_BANK_ACCOUNT, //银行密码
        SHOW_BANK_MONEY,//银行的钱
        SHOW_BANK_ACCOUNT_NAME,//银行的钱
        
        LAT, //纬度
        LNG,//经度
        TO_PAY_ORDER_ID,//待支付订单ID
        
        CODE_VERSION, //代码版本
        
        SIGN_TIME,//上一次 签到的时间
        SIGN_POINT,//存储下一次签到的  分数
        
        DEFAULT_DISPLAY,//默认显示的 首页
        USER_IM_ID,//IM id
        
        USER_CARD_NUM,//身份证号
        IGNORE_VERSION_ID,//忽略的版本名
        SHADE_PURCHASE_HALL,//采购大厅遮罩， 一次性
        SHADE_SUPPLY_HALL,//供应大厅遮罩， 一次性
        SHADE_DUMP_HALL,//甩货大厅遮罩， 一次性
        EC_CATEGORY_VERSION,//产品种类版本
        AREA_DB_VERSION,//省、市、区县数据库版本

        LIST_TYPE,//物流首页 list 默认显示的类型

        LATEST_MY_ORDER_TYPE,//我的订单，最近使用的订单类型
        LATEST_MY_ORDER_TAG,//我的订单，最近使用的Tag
        LATEST_SERVER_NAME,//记录上一次使用的域名

        DB_VERSION,//数据库版本

        //品牌直营。用户资料
        BRAND_SCOPR_AUDIT_STATUS,//审核状态
        BRAND_SCOPR_NAME,//姓名
        BRAND_SCOPR_SHOP_NAME,//商铺名称
        BRAND_SCOPR_AREA_ID,//城市ID
        BRAND_SCOPR_ADDRESS_DETAIL,//详细地址
        BRAND_SCOPR_RECEIVE_GOODS_TIME,//收货时间
    }

    public static int getInt(Field field, int defaultValue) {
        return SshApplication.getContext().getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).getInt(field.name(), defaultValue);
    };

    public static void setInt(Field field, int value) {
        Editor editor = SshApplication.getContext().getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).edit();
        editor.putInt(field.name(), value);
        editor.commit();
    }

    public static void setInt(Context context, String field, int value) {
        Editor editor = context.getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).edit();
        editor.putInt(field, value);
        editor.commit();
    }

    public static String getString(Field field, String defaultValue) {
    	String string = SshApplication.getContext().getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).getString(field.name(), defaultValue);
        return string;
    }

    public static void setString(Field field, String value) {
        Editor editor = SshApplication.getContext().getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).edit();
        editor.putString(field.name(), value);
        editor.commit();
    }

    public static String getString(String field, String defaultValue) {
        return SshApplication.getContext().getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).getString(field, defaultValue);
    }

    public static void setString(String field, String value) {
        Editor editor = SshApplication.getContext().getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).edit();
        editor.putString(field, value);
        editor.commit();
    }

    public static long getLong(Context context, Field field, long defaultValue) {
        return context.getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).getLong(field.name(), defaultValue);
    }

    public static void setLong(Context context, Field field, long value) {
        Editor editor = context.getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).edit();
        editor.putLong(field.name(), value);
        editor.commit();
    }

    public static boolean getBoolean(Context context, Field field, boolean defaultValue) {
        return context.getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).getBoolean(field.name(), defaultValue);
    }

    public static void setBoolean(Context context, Field field, boolean value) {
        Editor editor = context.getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).edit();
        editor.putBoolean(field.name(), value);
        editor.commit();
    }

    public static void setArraylong(Context context, Field field, ArrayList<Long> arrayLong) {
        if (arrayLong != null && arrayLong.size() > 0) {
            String str = "";
            for(int index = 0, count = arrayLong.size(); index < count; ++index) {
                str = str + arrayLong.get(index).toString() + SPLITCHAR;
            }
            Editor editor = context.getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).edit();
            editor.putString(field.name(), str);
            editor.commit();
        }
    }

    public static void removeKey(Context context, Field field) {
        Editor editor = context.getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).edit();
        editor.remove(field.name());
        editor.commit();
    }

    public static ArrayList<Long> getArrayLong(Context context, Field field, String defaultValue) {
        ArrayList<Long> arrayLong = new ArrayList<Long>();
        String str = context.getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).getString(field.name(), defaultValue);
        if (str != null) {
            String[] ids = str.split(SPLITCHAR);
            for(int index = 0; index < ids.length; ++index) {
                long id = Long.parseLong(ids[index]);
                arrayLong.add(id);
            }
        }
        return arrayLong;
    }

    public static void setArrayString(Context context, Field field, ArrayList<String> displayList) {
        String str = "";
        Integer num = 0;
        for(int index = 0, count = displayList.size(); index < count; ++index) {
            str = str + displayList.get(index) + SPLITCHAR;
            num++;
        }
        Editor editor = context.getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).edit();
        editor.putString(field.name(), str);
        editor.commit();
    }

    public static ArrayList<String> getArrayString(Context context, Field field, String defaultValue) {
        ArrayList<String> displayList = new ArrayList<String>();
        String str = context.getSharedPreferences(DATA_NAME, Context.MODE_PRIVATE).getString(field.name(), defaultValue);
        if (str != null) {
            String[] ids = str.split(SPLITCHAR);
            for(int index = 0; index < ids.length; index++) {
                if (!ids[index].equals(""))
                    displayList.add(ids[index]);
            }
        }
        return displayList;
    }

}
